﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Modules.crew.ui {
    import flash.events.*;
    import Engine.Managers.*;
    import flash.display.*;
    import Mechanics.*;
    import Events.*;
    import flash.utils.*;
    import org.aswing.*;
    import Classes.*;
    import Classes.util.*;
    import Classes.virals.*;
    import org.aswing.event.*;
    import org.aswing.border.*;
    import Modules.stats.experiments.*;
    import org.aswing.ext.*;
    import Display.aswingui.*;
    import Mechanics.GameEventMechanics.*;
    import Modules.stats.types.*;

    public class RollCallDialogCell extends JPanel implements GridListCell {

        private static const CHECKED_IN:String = "checkedIn";
        private static const NEEDS_REMINDER:String = "needsReminder";
        private static const POST_READY:String = "postReady";
        private static const YOU:String = "you";
        private static const SAMFRIEND:String = "samFriend";
        private static const MISSEDROLLCALL:String = "missedRollCall";

        protected var m_assetDict:Dictionary;
        protected var m_imageIconLoader:Loader;
        protected var m_imagePanel:JPanel;
        protected var m_data:Object;
        protected var m_statusHolder:JPanel;
        protected var m_state:String = "";
        protected var m_mechData:RollCallDataMechanic;
        protected var m_textField:JTextField;
        private var m_crewImage:JPanel;

        public function RollCallDialogCell(_arg1:Dictionary, _arg2:LayoutManager=null){
            this.m_assetDict = _arg1;
            super(new BorderLayout());
        }
        public function setGridListCellStatus(_arg1:GridList, _arg2:Boolean, _arg3:int):void{
        }
        public function setCellValue(_arg1):void{
            this.m_data = _arg1;
            this.m_mechData = (MechanicManager.getInstance().getMechanicInstance(this.m_assetDict["spawner"], "rollCall", MechanicManager.ALL) as RollCallDataMechanic);
            if (Global.isVisiting()){
                if (this.m_data["checkedIn"]){
                    this.m_state = CHECKED_IN;
                } else {
                    if (this.m_data.zid == Global.player.uid){
                        this.m_state = YOU;
                    } else {
                        if (this.m_mechData.isRollCallComplete()){
                            this.m_state = MISSEDROLLCALL;
                        } else {
                            if (this.m_data["feedSent"]){
                                this.m_state = NEEDS_REMINDER;
                            };
                        };
                    };
                };
            } else {
                if (this.m_data["checkedIn"]){
                    this.m_state = CHECKED_IN;
                } else {
                    if (!this.m_data["checkedIn"]){
                        if (this.m_mechData.isRollCallComplete()){
                            this.m_state = MISSEDROLLCALL;
                        } else {
                            if (this.m_data["zid"].substr(0, 1) == "-"){
                                this.m_state = SAMFRIEND;
                            } else {
                                if (!this.m_data["feedSent"]){
                                    this.m_state = POST_READY;
                                } else {
                                    this.m_state = NEEDS_REMINDER;
                                };
                            };
                        };
                    };
                };
            };
            this.buildCell();
        }
        public function getCellValue(){
            return (this.m_data);
        }
        public function getCellComponent():Component{
            return (this);
        }
        protected function makeBackground():void{
            var _local1:Sprite;
            if ((this.m_data["orderId"] % 2) == 1){
                _local1 = new Sprite();
                _local1.graphics.beginFill(EmbeddedArt.lightBlueTextColor);
                _local1.graphics.drawRect(0, 0, 681, 95);
                _local1.graphics.endFill();
                ASwingHelper.setSizedBackground(this, _local1);
            };
        }
        protected function buildCell():void{
            this.removeAll();
            this.makeBackground();
            this.append(this.makeLeftPanel(), BorderLayout.WEST);
            this.append(this.makeRightPanel(), BorderLayout.EAST);
        }
        protected function onImageLoaded(_arg1:Event):void{
            var _local2:DisplayObject;
            if (((this.m_imageIconLoader) && (this.m_imageIconLoader.content))){
                _local2 = this.m_imageIconLoader.content;
                _local2.scaleX = (_local2.scaleY = ASwingHelper.scaleToFit(_local2, this.m_imagePanel));
                if ((_local2 is Bitmap)){
                    (_local2 as Bitmap).smoothing = true;
                };
                this.m_imagePanel.append(new AssetPane(_local2));
            };
        }
        protected function makeRightPanel():JPanel{
            var _local1:JPanel = ASwingHelper.makeFlowJPanel(FlowLayout.RIGHT);
            var _local2:JPanel = ASwingHelper.makeSoftBoxJPanelVertical(SoftBoxLayout.CENTER, 2);
            var _local3:AssetPane = new AssetPane((new (this.m_assetDict["payroll_checkin_confirmCheckmark"])() as DisplayObject));
            var _local4:AssetPane = new AssetPane((new (this.m_assetDict["payroll_checkin_missingX"])() as DisplayObject));
            var _local5:AssetIcon = new AssetIcon((new EmbeddedArt.icon_cash() as DisplayObject));
            var _local6:JButton = new CustomButton(ZLoc.t("Dialogs", "RollCall_postRollCall"), null, "GreenButtonUI");
            _local6.setFont(new ASFont(EmbeddedArt.TITLE_FONT, 14, false, false, false, EmbeddedArt.advancedFontProps));
            var _local7:JButton = new CustomButton(ZLoc.t("Dialogs", "RollCall_remind"), null, "OrangeButtonUI");
            _local7.setFont(new ASFont(EmbeddedArt.TITLE_FONT, 14, false, false, false, EmbeddedArt.advancedFontProps));
            if (!this.canPostRollCallViral(this.m_data.zid)){
                _local6.setEnabled(false);
                _local7.setEnabled(false);
            };
            var _local8:int = Global.gameSettings.getInt("rollCallSkipCheckinPrice", 25);
            var _local9:JButton = new CustomButton(ZLoc.t("Dialogs", "RollCall_skip", {amount:String(_local8)}), _local5, "CashButtonUI");
            _local9.setFont(new ASFont(EmbeddedArt.TITLE_FONT, 14, false, false, false, EmbeddedArt.advancedFontProps));
            _local9.setMargin(new Insets(0, 5, 0, 5));
            var _local10:JButton = new CustomButton(ZLoc.t("Dialogs", "RollCall_checkinAndNotify"), null, "GreenButtonUI");
            var _local11:JButton = new CustomButton(ZLoc.t("Dialogs", "Collect"), null, "GreenButtonUI");
            var _local12:JButton = new CustomButton(ZLoc.t("Dialogs", "RollCall_replaceSam"), null, "GreenButtonUI");
            _local12.setFont(new ASFont(EmbeddedArt.TITLE_FONT, 14, false, false, false, EmbeddedArt.advancedFontProps));
            _local12.addActionListener(this.replaceSam, 0, true);
            _local6.addActionListener(this.postRollCallFeed, 0, true);
            _local7.addActionListener(this.sendReminder, 0, true);
            _local9.addActionListener(this.buyCheckin, 0, true);
            _local10.addActionListener(this.checkIn, 0, true);
            _local11.addActionListener(this.collect, 0, true);
            switch (this.m_state){
                case MISSEDROLLCALL:
                    _local2.appendAll(_local4);
                    break;
                case SAMFRIEND:
                    if (Global.isVisiting() == false){
                        _local2.appendAll(_local12, _local9);
                    };
                    break;
                case CHECKED_IN:
                    _local2.appendAll(_local3);
                    break;
                case POST_READY:
                    if (Global.isVisiting() == false){
                        _local2.appendAll(_local6, _local9);
                    };
                    break;
                case NEEDS_REMINDER:
                    if (Global.isVisiting() == false){
                        _local2.appendAll(_local7, _local9);
                    };
                    break;
                case YOU:
                    if (this.m_mechData.isRollCallComplete()){
                        _local2.appendAll(_local11);
                    } else {
                        _local2.appendAll(_local10);
                    };
                    break;
            };
            ASwingHelper.prepare(_local2);
            var _local13:int = ((95 - _local2.getHeight()) / 2);
            ASwingHelper.setEasyBorder(_local2, _local13);
            _local1.appendAll(_local2, ASwingHelper.horizontalStrut(10));
            return (_local1);
        }
        private function canPostRollCallViral(_arg1:String):Boolean{
            return ((Global.world.viralMgr.canPost(ViralType.ROLL_CALL_CHECKIN, _arg1) == true));
        }
        protected function makeLeftPanel():JPanel{
            var _local1:JPanel = ASwingHelper.makeFlowJPanel(FlowLayout.LEFT, 10);
            _local1.append(this.makeUser());
            _local1.append(this.makeBorder());
            _local1.append(this.makeUserStatus());
            return (_local1);
        }
        protected function makeBorder():AssetPane{
            var _local1:Sprite = new Sprite();
            _local1.graphics.beginFill(EmbeddedArt.rollCallBlue);
            _local1.graphics.drawRoundRect(0, 5, 3, 80, 2, 2);
            _local1.graphics.endFill();
            var _local2:AssetPane = new AssetPane(_local1);
            return (_local2);
        }
        protected function makeUser():JPanel{
            var _local1:JPanel = ASwingHelper.makeSoftBoxJPanelVertical(SoftBoxLayout.TOP, -5);
            var _local2:JLabel = ASwingHelper.makeLabel(this.m_data["orderId"], EmbeddedArt.TITLE_FONT, 36, EmbeddedArt.rollCallBlue);
            this.m_crewImage = ASwingHelper.makeFriendImageFromZid(this.m_data["zid"]);
            _local1.appendAll(_local2, this.m_crewImage);
            return (_local1);
        }
        protected function makeUserStatus():JPanel{
            var _local3:String;
            var _local5:Player;
            var _local1:JPanel = ASwingHelper.makeSoftBoxJPanelVertical(SoftBoxLayout.TOP, -5);
            var _local2:JLabel = ASwingHelper.makeLabel(this.m_data["position"], EmbeddedArt.DEFAULT_FONT_NAME_BOLD, 14, EmbeddedArt.blueTextColor);
            if (this.m_data["zid"].substr(0, 1) == "-"){
                _local3 = ZLoc.t("Main", "FakeFriendName");
            } else {
                if (this.m_data["zid"] == Global.player.uid){
                    _local3 = Global.player.name;
                } else {
                    if (Global.player.isFriendIDInList(this.m_data["zid"])){
                        _local5 = Global.player.findFriendById(this.m_data["zid"]);
                        _local3 = _local5.name;
                        if (((!(_local3)) || ((_local3.length == 0)))){
                            _local3 = ZLoc.t("RollCall", "FriendOfFriend", {firstName:Global.player.firstName});
                        };
                    };
                };
            };
            var _local4:JLabel = ASwingHelper.makeLabel(_local3, EmbeddedArt.DEFAULT_FONT_NAME_BOLD, 12, EmbeddedArt.brownTextColor);
            this.m_statusHolder = this.makeStatus();
            _local1.appendAll(ASwingHelper.leftAlignElement(_local2), ASwingHelper.leftAlignElement(_local4), ASwingHelper.verticalStrut(10), ASwingHelper.leftAlignElement(this.m_statusHolder));
            return (_local1);
        }
        protected function createTextField():JTextField{
            var _local1:String;
            if (this.m_data["zid"].substr(0, 1) == "-"){
                _local1 = ZLoc.t("Main", "FakeFriendName");
            } else {
                if (Global.player.isFriendIDInList(this.m_data["zid"])){
                    _local1 = Global.player.findFriendById(this.m_data["zid"]).name;
                };
            };
            this.m_textField = ASwingHelper.makeTextField(ZLoc.t("Dialogs", "RollCall_defaultMessage", {friendName:_local1}), EmbeddedArt.DEFAULT_SERIF_FONT, 12, EmbeddedArt.blueTextColor);
            this.m_textField.setEditable(true);
            this.m_textField.getTextField().wordWrap = true;
            this.m_textField.getTextField().multiline = true;
            this.m_textField.setFont(new ASFont("_sans", 12, false, false, false));
            this.m_textField.getTextField().selectable = true;
            this.m_textField.setMaxChars(200);
            this.m_textField.addEventListener(MouseEvent.MOUSE_UP, this.deleteDefaultText);
            this.m_textField.setBorder(new LineBorder(null, new ASColor(EmbeddedArt.blueTextColor), 2));
            this.m_textField.setPreferredWidth(280);
            this.m_textField.setPreferredHeight(47);
            this.m_textField.setBackground(ASwingHelper.getWhite());
            return (this.m_textField);
        }
        protected function makeStatus():JPanel{
            var _local2:JLabel;
            var _local1:JPanel = ASwingHelper.makeSoftBoxJPanelVertical(SoftBoxLayout.TOP);
            switch (this.m_state){
                case CHECKED_IN:
                    _local2 = ASwingHelper.makeLabel(ZLoc.t("Dialogs", "RollCall_checkedIn"), EmbeddedArt.TITLE_FONT, 24, EmbeddedArt.rollCallStatusBlue);
                    _local1.append(_local2);
                    break;
                case POST_READY:
                    if ((((Global.world.viralMgr.canPost(ViralType.ROLL_CALL_CHECKIN, this.m_data.zid) == true)) && (Global.player.findFriendById(this.m_data["zid"])))){
                        _local1.append(this.createTextField());
                    };
                    break;
                case NEEDS_REMINDER:
                    _local2 = ASwingHelper.makeLabel(ZLoc.t("Dialogs", "RollCall_missing"), EmbeddedArt.TITLE_FONT, 24, EmbeddedArt.rollCallStatusBlue);
                    _local1.append(_local2);
                    break;
                case MISSEDROLLCALL:
                    _local2 = ASwingHelper.makeLabel(ZLoc.t("Dialogs", "RollCall_missingFinal"), EmbeddedArt.TITLE_FONT, 24, EmbeddedArt.rollCallStatusBlue);
                    _local1.append(_local2);
                    break;
                case YOU:
                    break;
            };
            return (_local1);
        }
        private function postRollCallFeed(_arg1:AWEvent):void{
            var _local3:Number;
            var _local4:int;
            var _local5:String;
            var _local6:Boolean;
            var _local7:Boolean;
            var _local2:String = this.m_data["zid"];
            if (this.canPostRollCallViral(_local2)){
                _local3 = this.m_assetDict["spawner"].getId();
                _local4 = Global.experimentManager.getVariant(ExperimentDefinitions.EXPERIMENT_ROLL_CALL);
                _local5 = "";
                _local6 = this.deleteDefaultText(null);
                if (this.m_textField){
                    _local5 = this.m_textField.getText();
                };
                StatsManager.sample(100, StatsCounterType.DIALOG, "rollcalldialog", "post_roll_call", (_local6) ? "not_customized" : "customized");
                _local7 = true;
                if (_local4 == ExperimentDefinitions.ROLL_CALL_FEEDS){
                    _local7 = Global.world.viralMgr.sendRollCallCheckInFeed(_local2, {bid:_local3}, _local5, this.onFeedSent);
                };
                if (_local7){
                    this.m_mechData.feedSent(_local2);
                    this.m_state = NEEDS_REMINDER;
                };
                this.buildCell();
            };
        }
        private function deleteDefaultText(_arg1:MouseEvent):Boolean{
            var _local5:String;
            var _local2:Boolean;
            var _local3 = "";
            var _local4 = "";
            if (this.m_data["zid"].substr(0, 1) == "-"){
                _local4 = ZLoc.t("Main", "FakeFriendName");
            } else {
                if (Global.player.isFriendIDInList(this.m_data["zid"])){
                    _local4 = Global.player.findFriendById(this.m_data["zid"]).name;
                };
            };
            if (this.m_textField == null){
                _local5 = "";
            } else {
                _local5 = this.m_textField.getText();
            };
            var _local6:String = ZLoc.t("Dialogs", "RollCall_defaultMessage", {friendName:_local4});
            if (((this.m_textField) && ((_local5 == _local6)))){
                this.m_textField.setText("");
                _local2 = true;
            };
            return (_local2);
        }
        private function sendReminder(_arg1:AWEvent):void{
            var _local3:Number;
            var _local4:int;
            var _local5:String;
            var _local6:Boolean;
            var _local7:Boolean;
            var _local2:String = this.m_data["zid"];
            if (this.canPostRollCallViral(_local2)){
                _local3 = this.m_assetDict["spawner"].getId();
                _local4 = Global.experimentManager.getVariant(ExperimentDefinitions.EXPERIMENT_ROLL_CALL);
                _local5 = "";
                _local6 = this.deleteDefaultText(null);
                if (this.m_textField){
                    _local5 = this.m_textField.getText();
                };
                _local7 = true;
                StatsManager.sample(100, StatsCounterType.DIALOG, "rollcalldialog", "post_roll_call", (_local6) ? "not_customized" : "customized");
                if (_local4 == ExperimentDefinitions.ROLL_CALL_FEEDS){
                    _local7 = Global.world.viralMgr.sendRollCallCheckInFeed(_local2, {bid:_local3}, _local5, this.onFeedSent);
                };
                if (_local7){
                    this.m_state = NEEDS_REMINDER;
                };
                this.buildCell();
            };
        }
        private function onFeedSent(_arg1:int):void{
            dispatchEvent(new Event("onPostRollCall", true));
            var _local2:MechanicMapResource = (this.m_assetDict["spawner"] as MechanicMapResource);
            if (_local2){
                StatsManager.social("roll_call", this.m_data["zid"], "ask_for_check_in", _local2.getItemName(), _local2.getCrewPositionName(this.m_data["zid"]));
            };
        }
        private function buyCheckin(_arg1:AWEvent):void{
            dispatchEvent(new Event("onBuyCheckin", true));
            var _local2:int = Global.gameSettings.getInt("rollCallSkipCheckinPrice", 30);
            if (Global.player.cash < _local2){
                dispatchEvent(new Event("needMoreCash", true));
                return;
            };
            if (this.m_mechData.checkIn(this.m_data["zid"], true)){
                this.m_state = CHECKED_IN;
            };
            this.buildCell();
            dispatchEvent(new UIEvent(UIEvent.CHANGE_CREW_STATE, "", true));
        }
        private function replaceSam(_arg1:AWEvent):void{
            var _local4:Item;
            var _local5:XMLList;
            var _local2:String = ("crew.php?mId=" + this.m_mechData.owner.getId().toString());
            var _local3:MechanicMapResource = (this.m_assetDict["spawner"] as MechanicMapResource);
            if (_local3){
                _local4 = _local3.getItem();
                _local5 = _local4.getCurrentCrewGateXML(_local3);
                if (((((_local4) && (_local5))) && ((String(_local5.@name).length > 0)))){
                    _local2 = (_local2 + ("&gname=" + _local5.@name));
                };
            };
            FrameManager.navigateTo(_local2);
        }
        private function collect(_arg1:AWEvent):void{
            var _local3:TieredDooberMechanic;
            var _local4:String;
            dispatchEvent(new Event("onCollectBonus", true));
            Global.rollCallManager.collect(Global.player.uid, this.m_assetDict["spawner"]);
            var _local2:MechanicMapResource = (this.m_assetDict["spawner"] as MechanicMapResource);
            if (_local2){
                _local3 = (MechanicManager.getInstance().getMechanicInstance(_local2, "rollCallTieredDooberValue", MechanicManager.ALL) as TieredDooberMechanic);
                if (_local3){
                    _local4 = _local3.getRandomModifiersName();
                    StatsManager.social("roll_call", Global.world.ownerId, "collect_bonus", _local2.getItemName(), _local2.getCrewPositionName(Global.player.uid), _local4);
                };
            };
        }
        private function checkIn(_arg1:AWEvent):void{
            this.m_mechData.checkIn(Global.player.uid);
            if (((Global.isVisiting()) && (Global.world.viralMgr.canPost(ViralType.ROLL_CALL_NOTIFY_CHECKIN, Global.world.ownerId)))){
                Global.world.viralMgr.sendRollCallNotifyCheckInFeed(Global.world.ownerId, this.m_assetDict["spawner"]);
            };
            var _local2:MechanicMapResource = (this.m_assetDict["spawner"] as MechanicMapResource);
            if (_local2){
                StatsManager.social("roll_call", Global.world.ownerId, "checked_in", _local2.getItemName(), _local2.getCrewPositionName(Global.player.uid));
            };
            this.m_state = CHECKED_IN;
            this.buildCell();
        }

    }
}//package Modules.crew.ui 
